﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    Inherits="Pages_Reservation_ReservationEdit" CodeBehind="ReservationEdit.aspx.cs" %>

<%@ Register Src="Ascx/ProgramPopup.ascx" TagName="ProgramPopup" TagPrefix="uc1" %>
<%@ Register Src="Ascx/PaymentPopup.ascx" TagName="PaymentPopup" TagPrefix="uc2" %>
<%@ Register Src="Ascx/ChangeReservationPopup.ascx" TagName="ChangeReservationPopup"
    TagPrefix="uc3" %>
<%@ Register Src="~/Tools/NumericBox.ascx" TagName="NumericBox" TagPrefix="num" %>
<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="Server">
    Reservation management
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="using" runat="Server">

    <script type="text/javascript" src="/js/jquery.metadata.js"></script>

    <script type="text/javascript" src="/js/autoNumeric-1.7.4.js"></script>

    <script type="text/javascript" src="/Pages/Reservations/Js/reservation-lib.js"></script>

</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="header" runat="Server">
    <uc1:ProgramPopup ID="ProgramPopup" runat="server" />
    <uc2:PaymentPopup ID="PaymentPopup" runat="server" />
    <uc3:ChangeReservationPopup ID="ChangeReservationPopup" runat="server" />
    <a href="Reservation.aspx" title="Back Button"><span id="backbutton">Back</span></a>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="content" runat="Server">
    <div style="margin: 15px">
        <!-- Operator Information -->
        <fieldset style="padding: 10px;">
            <legend>
                <h1>
                    Operator Info</h1>
            </legend>
            <table>
                <tr>
                    <td colspan="2">
                        <div class="item">
                            <div class="label">
                                Booking code:
                            </div>
                            <div class="field">
                                <em>*</em><asp:TextBox ID="txtBookingCode" runat="server" Width="186px" CssClass="required"></asp:TextBox>
                                <asp:Label ID="lblAlert" runat="server" ForeColor="Red">This agent is exist! </asp:Label>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="This field is required."
                                    InitialValue="" ControlToValidate="txtBookingCode"></asp:RequiredFieldValidator>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Created by:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblCreatedBy" runat="server"></asp:Label>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Created date:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblCreationDate" runat="server"></asp:Label>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Modified by:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblModifiedBy" runat="server"></asp:Label>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Modified date:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblModifiedDate" runat="server"></asp:Label>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        <!-- Contact Information -->
        <fieldset style="padding: 10px;">
            <legend>
                <h1>
                    Contact Information</h1>
            </legend>
            <table>
                <tr>
                    <td colspan="2">
                        <div class="divAgent item" style="display: none;">
                            <div class="label">
                                Agent#:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblAgent" runat="server"></asp:Label>
                                <asp:HiddenField ID="hdfAgentID" runat="server" />
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div id="divAgent" class="divAgent item" runat="server">
                            <div class="label">
                                Company name:
                            </div>
                            <div class="field">
                                <asp:Label ID="lblCompanyName" runat="server" Width="365" Font-Bold="true" ForeColor="Blue"></asp:Label>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div id="divWalkIn" class="item" runat="server">
                            <div class="label">
                                Title:
                            </div>
                            <div class="field">
                                <asp:DropDownList ID="ddlTitle" runat="server">
                                    <asp:ListItem>Ms.</asp:ListItem>
                                    <asp:ListItem>Mr.</asp:ListItem>
                                    <asp:ListItem>Mrs.</asp:ListItem>
                                </asp:DropDownList>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                First name:</div>
                            <div class="field">
                                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
                                    ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Last name:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*"
                                    ControlToValidate="txtLastName"></asp:RequiredFieldValidator>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="item">
                            <div class="label">
                                Address Line 1:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtAddressLine1" Style="width: 350px" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <div class="item">
                            <div class="label">
                                Address Line 2:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtAddressLine2" Style="width: 350px" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Country:
                            </div>
                            <div class="field">
                                <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">
                                    <asp:ListItem Text="Choose a country ... " Value=""></asp:ListItem>
                                </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                                    InitialValue="" ControlToValidate="ddlCountry"></asp:RequiredFieldValidator>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                City:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                ZipCode:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtZipCode" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Email:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*"
                                    InitialValue="" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*"
                                    ControlToValidate="txtEmail" ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"></asp:RegularExpressionValidator>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Tel.:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtTel" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Fax:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtFax" runat="server"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </fieldset>
        <!-- Reservation Information -->
        <fieldset style="padding: 10px;">
            <legend class="subtitle">Reservation Info</legend>
            <table>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Boat:
                            </div>
                            <div class="field">
                                <asp:Literal ID="LiteralBoat" runat="server"></asp:Literal>
                                <asp:HiddenField ID="hdfBoatID" runat="server" />
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Booking status:
                            </div>
                            <div class="field">
                                <asp:DropDownList runat="server" ID="ddlBookingStatus">
                                    <asp:ListItem Value="0">In progress</asp:ListItem>
                                    <asp:ListItem Value="1">Cancelled</asp:ListItem>
                                </asp:DropDownList>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                N&#186; of nights:
                            </div>
                            <div class="field">
                                <asp:Literal ID="LiteralDateNumber" runat="server"></asp:Literal>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                N&#186; of cabins:
                            </div>
                            <div id="noOfCabins" class="field">
                                <asp:Literal ID="LiteralCabinNumber" runat="server"></asp:Literal>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Departure date:
                            </div>
                            <div class="field">
                                <asp:Literal ID="litDepDate" runat="server"></asp:Literal>
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="item">
                            <div class="label">
                                Board date:
                            </div>
                            <div class="field">
                                <asp:Literal ID="litBoardDate" runat="server"></asp:Literal>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="item">
                            <div class="label">
                                Alarm:
                            </div>
                            <div class="field">
                                <asp:TextBox ID="txtAlarm" runat="server" CssClass="datepicker"></asp:TextBox>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:TextBox ID="txtNote" runat="server" TextMode="MultiLine" Height="200" Width="510px"
                            Rows="10"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <a id="btnChange" class="button" href="javascript:void(0)"><span>Change</span></a>
                        <a href="javascript:window.scrollTo(0, 20000);" class="button"><span>Go to bottom</span></a>
                    </td>
                </tr>
            </table>
        </fieldset>
        <!-- Cabins Information -->
        <fieldset style="padding: 10px;">
            <legend class="subtitle">Cabin Info</legend>
            <asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Cabin:</div>
                                    <div class="field">
                                        <asp:TextBox ID="txtCabinNumber" runat="server" ReadOnly="true"></asp:TextBox>
                                        <asp:TextBox ID="txtCabinID" runat="server" Style="display: none" />
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Boat:</div>
                                    <div class="field">
                                        <asp:Literal ID="litBoatName" runat="server"></asp:Literal>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Representative:
                                    </div>
                                    <div class="field">
                                        <asp:TextBox ID="txtRepresentative" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="*"
                                            InitialValue="" ControlToValidate="txtRepresentative"></asp:RequiredFieldValidator>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Status:
                                    </div>
                                    <div class="field">
                                        <asp:DropDownList ID="ddlStatus" runat="server" CssClass="ddlStatus">
                                            <asp:ListItem Value="1">Booked</asp:ListItem>
                                            <asp:ListItem Value="2">Blocked</asp:ListItem>
                                            <asp:ListItem Value="3">Release</asp:ListItem>
                                        </asp:DropDownList>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Type of cabin:
                                    </div>
                                    <div class="field">
                                        <asp:DropDownList ID="ddlBedrooms" runat="server">
                                            <asp:ListItem Value="1">Double</asp:ListItem>
                                            <asp:ListItem Value="2">Twin</asp:ListItem>
                                            <asp:ListItem Value="3">Single</asp:ListItem>
                                        </asp:DropDownList>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Block dates:
                                    </div>
                                    <div class="field">
                                        <asp:DropDownList ID="ddlBlockDates" runat="server">
                                            <asp:ListItem>1</asp:ListItem>
                                            <asp:ListItem>2</asp:ListItem>
                                            <asp:ListItem>3</asp:ListItem>
                                            <asp:ListItem>4</asp:ListItem>
                                            <asp:ListItem>5</asp:ListItem>
                                            <asp:ListItem>6</asp:ListItem>
                                            <asp:ListItem>7</asp:ListItem>
                                            <asp:ListItem>8</asp:ListItem>
                                            <asp:ListItem>9</asp:ListItem>
                                            <asp:ListItem>10</asp:ListItem>
                                            <asp:ListItem>11</asp:ListItem>
                                            <asp:ListItem>12</asp:ListItem>
                                            <asp:ListItem>13</asp:ListItem>
                                            <asp:ListItem>14</asp:ListItem>
                                            <asp:ListItem>15</asp:ListItem>
                                        </asp:DropDownList>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        Price:
                                    </div>
                                    <div class="field">
                                        <asp:TextBox ID="txtPrice" runat="server" Width="100" onblur="getBoatingAmount()"
                                            Text="0" />
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="item">
                                    <div class="label">
                                        # of Pax:
                                    </div>
                                    <div class="field">
                                        <asp:DropDownList ID="ddlNumberClient" runat="server" onchange="changeNumOfPax()">
                                            <asp:ListItem>0</asp:ListItem>
                                            <asp:ListItem>1</asp:ListItem>
                                            <asp:ListItem>2</asp:ListItem>
                                            <asp:ListItem>3</asp:ListItem>
                                            <asp:ListItem>4</asp:ListItem>
                                            <asp:ListItem>5</asp:ListItem>
                                            <asp:ListItem>6</asp:ListItem>
                                            <asp:ListItem>7</asp:ListItem>
                                            <asp:ListItem>8</asp:ListItem>
                                            <asp:ListItem>9</asp:ListItem>
                                            <asp:ListItem>10</asp:ListItem>
                                        </asp:DropDownList>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <div class="item">
                                    <div class="label">
                                        Notes:
                                    </div>
                                    <div class="field">
                                        <asp:TextBox ID="txtNote" runat="server" TextMode="MultiLine" Height="100" Width="410px"
                                            Text='<%#Eval("Note") %>'></asp:TextBox>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
            <div class="item">
                <div class="label">
                    Email Confirm:</div>
                <div class="field">
                    <asp:TextBox ID="txtEmailConfirmation" runat="server" CssClass="email"></asp:TextBox>
                </div>
            </div>
            <div style="padding-left: 250px">
                <a class="button" id="btnSave" href="javascript:void(0)"><span>Save</span> </a><a
                    class="button" href="javascript:void(0)" title="Save & Confirm" id="btnSaveNConfirm"
                    style="display: none"><span>Save & Confirm</span></a><a class="button" href="javascript:void(0)"
                        title="Send confirmation" id="btnSendMail" style="display: none"> <span>Send confirmation</span></a>
            </div>
        </fieldset>
        <fieldset style="padding: 10px;">
            <legend class="subtitle">Payment History</legend>
            <div id="divPaymentHistory">
            </div>
        </fieldset>
        <fieldset style="padding: 10px;">
            <table style="float: right; width: 300px; line-height: 30px;" cellspacing="0" cellpadding="0">
                <tbody>
                    <tr>
                        <td>
                            # of pax:
                            <asp:TextBox ID="txtTotalClients" runat="server" CssClass="integer" MaxLength="3"
                                Text="0"></asp:TextBox>
                        </td>
                        <td>
                            x
                            <asp:CheckBox ID="chkShuttle" runat="server" Text="(Shuttle)" />
                        </td>
                        <td>
                            <num:NumericBox ID="txtShuttlePrice" runat="server" Text="0" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            Cruise package:
                        </td>
                        <td>
                            <num:NumericBox ID="txtBoatingFee" runat="server" Text="0" />
                        </td>
                    </tr>
                    <tr style="height: 15px">
                        <td>
                        </td>
                        <td colspan="2">
                            <hr />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td style="font-size: x-large; font-weight: bolder;">
                            &Sigma;<asp:DropDownList ID="ddlReservationCurrency" runat="server" Width="68px"
                                Font-Bold="true" Style="border: 0">
                            </asp:DropDownList>
                            <asp:HiddenField ID="txtReservationRate" runat="server" Value="1"></asp:HiddenField>
                        </td>
                        <td>
                            <num:NumericBox ID="txtTotal" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                    <tr style="height: 15px">
                        <td colspan="3">
                            <hr style="border-color: Red" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            Sub-Total:
                        </td>
                        <td>
                            <num:NumericBox ID="txtSubTotal" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                    <tr style="color: Red">
                        <td>
                        </td>
                        <td>
                            Deposit:
                        </td>
                        <td>
                            <num:NumericBox ID="txtDeposit" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                    <tr style="color: Green">
                        <td>
                        </td>
                        <td>
                            Paid:
                        </td>
                        <td>
                            <num:NumericBox ID="txtPaid" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                    <tr style="color: Gray">
                        <td>
                        </td>
                        <td>
                            Fee:
                        </td>
                        <td>
                            <num:NumericBox ID="txtPaymentFee" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                    <tr style="color: Blue">
                        <td>
                        </td>
                        <td>
                            Balance:
                        </td>
                        <td>
                            <num:NumericBox ID="txtBalance" runat="server" Text="0" Enabled="false" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="item" style="display: none">
                <div class="label">
                    Payment complete:
                </div>
                <div class="field">
                    <asp:CheckBox ID="chkPaidComplete" runat="server" CssClass="checkbox" />
                </div>
            </div>
            <asp:HiddenField ID="hdfCharterPrice" runat="server" Value="0" />
        </fieldset>
        <p class="back-top">
            <a href="#top"><span></span></a>
        </p>
        <p class="go-bottom">
            <a href="#bottom"><span></span></a>
        </p>
    </div>

    <script type="text/javascript">

        $(document).ready(function () {
            $("#<%=lblAlert.ClientID %>").hide();

            // Save and confirm
            $("#btnSave").click(function () { Save('Save'); });
            $("#btnSaveNConfirm").click(function () {
                if ($.trim($('#<%=txtEmailConfirmation.ClientID %>').val()) == '') {
                    alert('Please input the Email Confirmation to receive the confirmation mail. Thank you!');
                }
                else
                    Save('SaveConfirm');
            });

            $("#btnSendMail").click(function () {
                window.location = "/Pages/Reservations/EmailConfirmation.aspx?ReservationID=" + '<%=ReservationID %>';
            });

            $("#<%=ddlReservationCurrency.ClientID %>").change(function () {
                var rate = null;
                if ($('#<%=divWalkIn.ClientID %>').is(':visible')
                || $("#<%=hdfAgentID.ClientID %>").val() == '<%=Germany %>')
                    rate = getRate($("#<%=ddlReservationCurrency.ClientID %> option:selected").text());
                else
                    rate = getCash($("#<%=ddlReservationCurrency.ClientID %> option:selected").text());

                if (rate != null) {
                    // set the exchange rate
                    $("#<%=txtReservationRate.ClientID %>").val(rate.UnitPrice);
                }

                getTotal()
            });


            $(".cabinnumber").autocomplete({
                source: function (request, response) {
                    var cabins = '';
                    var cabinid = $(".cabinid");
                    cabinid.each(function () {
                        cabins += ',' + $(this).val();
                    });

                    var params = "{ boatID: '" + '<%=BoatID %>' + "', boardDateString: '" + '<%=BoardDate %>' + "', noOfNights: " + '<%=NoOfNights %>' + ", cabins: '" + cabins + "' }";
                    $.ajax({
                        url: "/Get.asmx/GetAvailableCabins",
                        data: params,
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    value: item.CabinNumber,
                                    obj: item
                                }
                            }));
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('Get the free cabins failed.');
                        }
                    });
                },
                select: function (event, ui) {
                    var index = $(this).attr("index");
                    $(".cabinid-" + index).val(ui.item.obj.CabinID);
                },
                minLength: 1
            });

            loadPayment();

            $('input.auto').autoNumeric({ aSep: ',', aDec: '.', aPad: false, vMin: '-999999999.99' });
            $('input.auto').focus(function () { if (this.value == '0') this.value = ''; });
            $('input.auto').blur(function () { if (this.value == '') this.value = '0'; });
        });

        /// <summary>
        /// Get Total
        /// </summary>
        function getTotal() {
            var total = toFloat($("#<%=txtShuttlePrice.ClientID %>").autoNumericGet()) +
                        toFloat($("#<%=txtBoatingFee.ClientID %>").autoNumericGet());

            $("#<%=txtTotal.ClientID %>").autoNumericSet(total);

            getSubTotal();
        }

        /// <summary>
        /// Get Sub total
        /// </summary>
        function getSubTotal() {

            var total = $("#<%=txtTotal.ClientID %>").autoNumericGet();
            var subTotal = total * $("#<%=txtReservationRate.ClientID %>").autoNumericGet();

            $("#<%=txtSubTotal.ClientID %>").autoNumericSet(subTotal);

            var sumDeposit = 0;
            var sumPaidAmount = 0;
            var sumFeeAmount = 0;
            var tbody = $('#gridPaymentHistory > tbody');
            $('tr.special', tbody).each(function () {
                // Check deposit
                if ($.trim($(this).children("td:eq(4)").text()).toLowerCase() == 'yes')
                    sumDeposit += toFloat($(this).children("td:eq(2)").text());
                else
                    sumPaidAmount += toFloat($(this).children("td:eq(2)").text());
                sumFeeAmount += toFloat($(this).children("td:eq(3)").text());
            });
            $("#<%=txtDeposit.ClientID %>").autoNumericSet(sumDeposit);
            $("#<%=txtPaid.ClientID %>").autoNumericSet(sumPaidAmount);
            $("#<%=txtPaymentFee.ClientID %>").autoNumericSet(sumFeeAmount);
            $("#<%=txtBalance.ClientID %>").autoNumericSet(subTotal - sumDeposit - sumPaidAmount);
            //            $('input.currency').formatCurrency();
        }

        /// <summary>
        /// Calculate the Cruise Package
        /// </summary>
        function getBoatingAmount() {
            var boatingAmount = $("input.price").sumCruisePackage();
            $("#<%=txtBoatingFee.ClientID %>").autoNumericSet(boatingAmount);

            getTotal();
        }

        /// <summary>
        /// Change number of pax
        /// </summary>
        function changeNumOfPax() {
            var numOfPax = $("select.numberclient").sumValues();
            $("#<%=txtTotalClients.ClientID %>").autoNumericSet(numOfPax);

            getBoatingAmount();
        }

        /// <summary>
        /// Get the guest WI info
        /// </summary>
        function getGuest() {
            var item;

            item = {
                GuestID: $("#<%=hdfAgentID.ClientID %>").val(),
                Active: true,
                AddressLine1: $("#<%=txtAddressLine1.ClientID %>").val(),
                AddressLine2: $("#<%=txtAddressLine2.ClientID %>").val(),
                City: $("#<%=txtCity.ClientID %>").val(),
                CompanyName: $("#<%=lblCompanyName.ClientID %>").text(),
                CountryID: $("#<%=ddlCountry.ClientID %> option:selected").val(),
                Email: $("#<%=txtEmail.ClientID %>").val(),
                Fax: $("#<%=txtFax.ClientID %>").val(),
                FirstName: $("#<%=txtFirstName.ClientID %>").val(),
                Lastname: $("#<%=txtLastName.ClientID %>").val(),
                Tel: $("#<%=txtTel.ClientID %>").val(),
                Title: $("#<%=ddlTitle.ClientID %> option:selected").val(),
                ZipCode: $("#<%=txtZipCode.ClientID %>").val()
            };

            return item;
        }

        /// <summary>
        /// Get the reservation
        /// </summary>
        function getReservarion() {
            var item = {
                ReservationID: '<%=ReservationID %>',
                ReservationCode: $("#<%=txtBookingCode.ClientID %>").val(),
                Status: parseInt($("#<%=ddlBookingStatus.ClientID %> option:selected").val()),
                DepartureDate: '<%=DepDate.ToShortDate() %>',
                CheckInDate: '<%=BoardDate.ToShortDate() %>',
                IsShuttle: $("#<%=chkShuttle.ClientID %>").is(":checked"),
                ShuttlePrice: $("#<%=txtShuttlePrice.ClientID %>").autoNumericGet(),
                CruisePackage: $("#<%=txtBoatingFee.ClientID %>").autoNumericGet(),
                CruiseRate: $("#<%=txtReservationRate.ClientID %>").autoNumericGet(),
                CruiseCurrency: parseInt($("#<%=ddlReservationCurrency.ClientID %> option:selected").val()),
                NumOfNights: toFloat('<%=NoOfNights %>'),
                NumOfPax: parseInt($("#<%=txtTotalClients.ClientID %>").val()),
                SubTotal: $("#<%=txtSubTotal.ClientID %>").autoNumericGet(),
                Deposit: $("#<%=txtDeposit.ClientID %>").autoNumericGet(),
                Paid: $("#<%=txtPaid.ClientID %>").autoNumericGet(),
                PaidFee: $("#<%=txtPaymentFee.ClientID %>").autoNumericGet(),
                Alarm: $("#<%=txtAlarm.ClientID %>").val(),
                PaidStatus: $("#<%=chkPaidComplete.ClientID %>").is(":checked"),
                Note: $("#<%=txtNote.ClientID %>").val(),
                ConfirmationEmail: $("#<%=txtEmailConfirmation.ClientID %>").val()
            }
            return item;
        }

        /// <summary>
        /// Load payment history
        /// </summary>
        function loadPayment() {
            var reservationID = '<%=ReservationID %>';
            var params = "{ reservaiontID: '" + reservationID + "'}";
            $.ajax({
                type: "POST",
                url: "/Get.asmx/GetPaymentList",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: params,
                success: function (data) {
                    var payments = { List: [] };
                    if (data.d != null && data.d.List != null) payments = data.d;
                    $('#divPaymentHistory').setTemplateURL("/Pages/Reservations/template/paymentHistoryTemplate.htm");
                    $('#divPaymentHistory').processTemplate(payments);

                    // Insert
                    $("#gridPaymentHistory a.btnNew").button().click(function () {

                        clearPayment();

                        $("#payment-dialog").dialog("open");
                    });

                    paymentEvents();

                    return true;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    var err = eval("(" + XMLHttpRequest.responseText + ")");
                    alert("Load the payment of this booking failed! Detail: " + err.Message);
                    return false;
                }
            });
        }

        /// <summary>
        /// Save function
        /// </summary>
        function Save(state) {
            if (!validation()) return;

            var updateUrl = '', redirectUrl = '', message = '', params = null;

            var guest = getGuest();
            var paymentBatches = getPaymentBatches();
            var reservation = getReservarion();
            getReservationDetails(reservation);

            var jsonGuest = JSON.stringify(guest);
            var jsonPayment = JSON.stringify(paymentBatches);
            var jsonReservation = JSON.stringify(reservation);
            var email = "'" + $('#<%=txtEmailConfirmation.ClientID %>').val() + "'";

            switch (state) {
                case 'Save':
                    updateUrl = "/Update.asmx/UpdateReservation";
                    redirectUrl = "/Pages/Reservations/ReservationList.aspx";
                    message = "Save the booking failed. Details: ";
                    params = "{ reservation: " + jsonReservation + ", guest: " + jsonGuest + ", paymentBatches: " + jsonPayment + " }";
                    break;
                case 'SaveConfirm':
                    updateUrl = "/Update.asmx/UpdateConfirmReservation";
                    redirectUrl = "/Pages/Reservations/EmailConfirmation.aspx?ReservationID=" + '<%=ReservationID %>';
                    message = "Save and confirmation the booking failed. Details: ";
                    params = "{ reservation: " + jsonReservation + ", guest: " + jsonGuest + ", paymentBatches: " + jsonPayment + ", email: " + email + " }";
                    break;
            }

            $.ajax({
                url: updateUrl,
                data: params,
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                async: false,
                success: function (data) {
                    result = data.d;
                    if (result)
                        window.location = redirectUrl;
                    else
                        alert('Update failed');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    var err = eval("(" + XMLHttpRequest.responseText + ")");
                    alert(message + err.Message);
                }
            });
        }

        var validControl = $([]).add($("#<%=txtBookingCode.ClientID %>"));

        /// <summary>
        /// Validation the controls
        /// </summary>
        function validation() {
            var b = true;
            var first = true;
            validControl.each(function () {
                b &= $(this).valid();
                if (!b && first) {
                    $(this).focus();
                    first = false;
                }
            });
            if (!b) return false;

            if (checkBooking($("#<%=txtBookingCode.ClientID %>").val(), '<%=ReservationID %>')) {
                $("#<%=lblAlert.ClientID %>").text("This booking code is exists.");
                $("#<%=lblAlert.ClientID %>").css('display', '');
                return false;
            }

            return true;
        }
    </script>

</asp:Content>
